package com.zdft.promotion;

import com.zdft.esb.enter.EsbClient;
import com.zdft.esb.servlet.ZBusMicHttpServerServlet;
import com.zdft.promotion.common.acl.ControllerACLAnnotationHandler;
import com.ztft.zbus.tts.constant.ZtsAppInfo;
import com.ztft.zbus.tts.servlet.TriggeredServerServlet;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.context.WebApplicationContext;

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@ComponentScan("com.zdft")
public class PromotionApplication extends SpringBootServletInitializer {

    @Value("${esbHost}")
    public String esbClientHost;

    @Value("${esbPort}")
    public Integer esbClientPort;

    @Override
    protected WebApplicationContext run(SpringApplication application) {
        application.addListeners(new ControllerACLAnnotationHandler());
        return super.run(application);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(PromotionApplication.class);
    }

    @Bean
    public ServletRegistrationBean getZBusEsbHttpServerServletRegistrationBean() {
        ServletRegistrationBean bean = new ServletRegistrationBean(new ZBusMicHttpServerServlet());
        bean.addUrlMappings("/" + EsbClient.zbusMicHttpServerUrlMappings);
        return bean;
    }

    @Bean
    public EsbClient esbClient() {
        return new EsbClient(esbClientHost, esbClientPort);
    }

    @Bean
    public ServletRegistrationBean getZtsServerServletRegistrationBean() {
        ServletRegistrationBean bean = new ServletRegistrationBean(new TriggeredServerServlet());
        bean.addUrlMappings("/" + ZtsAppInfo.triggeredServerUrlMappings);
        return bean;
    }

    public static void main(String[] args) {
        System.setProperty("es.set.netty.runtime.available.processors", "false");
        SpringApplication.run(PromotionApplication.class, args);
    }

}
